﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;

namespace Blokus.Scenes
{
    public class SceneControls : Scene
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        private SpriteFont menuFont;
        private KeyboardState keyPrevState;
        private GamePadState gpPrevState;
        private Texture2D texBG;
        private float aspectRatio;
        private RenderTarget2D backgroundRenderTarget;
        public static string[] menuItems;
        public static string[] controlItems;
        public static string[] xboxItems;
        public static int currentIndex = 0;
        public static bool changingKey = false;
        public static Keys keySoft = Keys.Down, keyHard = Keys.Up, keyLeft = Keys.Left, keyRight = Keys.Right,
            keyFlipLeft = Keys.Z, keyFlipRight = Keys.X;
        public static Buttons buttonSoft = Buttons.DPadDown, buttonHard = Buttons.DPadUp, buttonLeft = Buttons.DPadLeft,
            buttonRight = Buttons.DPadRight, buttonFlipLeft = Buttons.LeftTrigger, buttonFlipRight = Buttons.RightTrigger;


        public SceneControls()
        {
            menuItems = new string[9];
            controlItems = new string[6];
            xboxItems = new string[6];
            menuItems[0] = "Move Left"; controlItems[0] = "Left"; xboxItems[0] = "DPadLeft";
            menuItems[1] = "Move Right"; controlItems[1] = "Right"; xboxItems[1] = "DPadRight";
            menuItems[2] = "Soft Drop"; controlItems[2] = "Down"; xboxItems[2] = "DPadDown";
            menuItems[3] = "Hard Drop"; controlItems[3] = "Up"; xboxItems[3] = "DPadUp";
            menuItems[4] = "Flip Left"; controlItems[4] = "Z"; xboxItems[4] = "LeftTrigger";
            menuItems[5] = "Flip Right"; controlItems[5] = "X"; xboxItems[5] = "RightTrigger";
            menuItems[6] = "Back To Menu";
            menuItems[7] = "";
            menuItems[8] = "(Press Enter/A on an item to change the control)";
        }

        public override void LoadContent(ContentManager content, GraphicsDeviceManager graphics)
        {
            this.graphics = graphics;
            //load font
            menuFont = content.Load<SpriteFont>("gamefont");
            backgroundRenderTarget = new RenderTarget2D(graphics.GraphicsDevice, 1280, 800);
            spriteBatch = new SpriteBatch(graphics.GraphicsDevice);

            texBG = content.Load<Texture2D>("Textures\\background");
            aspectRatio = graphics.GraphicsDevice.Viewport.AspectRatio;


        }

        public override Blokus.GameState Update(GameTime gameTime)
        {
            GamePadState currGPState = GamePad.GetState(PlayerIndex.One);
            //check if new key is assigned
            if (changingKey && (Keyboard.GetState() != keyPrevState && Keyboard.GetState().GetPressedKeys().Count() > 0 || currGPState != gpPrevState))
            {
#if XBOX360
                //check each button
                bool finished = false;
                Buttons newButton = Buttons.A;
                while (!finished) {
                    if (currGPState.IsButtonDown(Buttons.A)) {
                        newButton = Buttons.A;
                        break;
                    }
                    else if (currGPState.IsButtonDown(Buttons.B)) {
                        newButton = Buttons.B;
                        break;
                    }
                    else if (currGPState.IsButtonDown(Buttons.Back)) {
                        newButton = Buttons.Back;
                        break;
                    }
                    else if (currGPState.IsButtonDown(Buttons.BigButton)) {
                        newButton = Buttons.BigButton;
                        break;
                    }
                    else if (currGPState.IsButtonDown(Buttons.DPadDown)) {
                        newButton = Buttons.DPadDown;
                        break;
                    }
                    else if (currGPState.IsButtonDown(Buttons.DPadLeft)) {
                        newButton = Buttons.DPadLeft;
                        break;
                    }
                    else if (currGPState.IsButtonDown(Buttons.DPadRight)) {
                        newButton = Buttons.DPadRight;
                        break;
                    }
                    else if (currGPState.IsButtonDown(Buttons.DPadUp)) {
                        newButton = Buttons.DPadUp;
                        break;
                    }
                    else if (currGPState.IsButtonDown(Buttons.LeftShoulder)) {
                        newButton = Buttons.LeftShoulder;
                        break;
                    }
                    else if (currGPState.IsButtonDown(Buttons.LeftStick)) {
                        newButton = Buttons.LeftStick;
                        break;
                    }
                    else if (currGPState.IsButtonDown(Buttons.LeftThumbstickDown)) {
                        newButton = Buttons.LeftThumbstickDown;
                        break;
                    }
                    else if (currGPState.IsButtonDown(Buttons.LeftThumbstickLeft)) {
                        newButton = Buttons.LeftThumbstickLeft;
                        break;
                    }
                    else if (currGPState.IsButtonDown(Buttons.LeftThumbstickRight)) {
                        newButton = Buttons.LeftThumbstickRight;
                        break;
                    }
                    else if (currGPState.IsButtonDown(Buttons.LeftThumbstickUp)) {
                        newButton = Buttons.LeftThumbstickUp;
                        break;
                    }
                    else if (currGPState.IsButtonDown(Buttons.LeftTrigger)) {
                        newButton = Buttons.LeftTrigger;
                        break;
                    }
                    else if (currGPState.IsButtonDown(Buttons.RightShoulder)) {
                        newButton = Buttons.RightShoulder;
                        break;
                    }
                    else if (currGPState.IsButtonDown(Buttons.RightStick)) {
                        newButton = Buttons.RightStick;
                        break;
                    }
                    else if (currGPState.IsButtonDown(Buttons.RightThumbstickDown)) {
                        newButton = Buttons.RightThumbstickDown;
                        break;
                    }
                    else if (currGPState.IsButtonDown(Buttons.RightThumbstickLeft)) {
                        newButton = Buttons.RightThumbstickLeft;
                        break;
                    }
                    else if (currGPState.IsButtonDown(Buttons.RightThumbstickRight)) {
                        newButton = Buttons.RightThumbstickRight;
                        break;
                    }
                    else if (currGPState.IsButtonDown(Buttons.RightThumbstickUp)) {
                        newButton = Buttons.RightThumbstickUp;
                        break;
                    }
                    else if (currGPState.IsButtonDown(Buttons.RightTrigger)) {
                        newButton = Buttons.RightTrigger;
                        break;
                    }
                    else if (currGPState.IsButtonDown(Buttons.Start)) {
                        newButton = Buttons.Start;
                        break;
                    }
                    else if (currGPState.IsButtonDown(Buttons.X)) {
                        newButton = Buttons.X;
                        break;
                    }
                    else if (currGPState.IsButtonDown(Buttons.Y)) {
                        newButton = Buttons.Y;
                        break;
                    }
                    finished = true;
                }
                if (!finished) {
                    if (currentIndex == 0)
                    { //move left
                        buttonLeft = newButton;
                        xboxItems[currentIndex] = newButton.ToString();
                    }
                    else if (currentIndex == 1)
                    { //move right
                        buttonRight = newButton;
                        xboxItems[currentIndex] = newButton.ToString();
                    }
                    else if (currentIndex == 2)
                    { //soft drop
                        buttonSoft = newButton;
                        xboxItems[currentIndex] = newButton.ToString();
                    }
                    else if (currentIndex == 3)
                    { //hard drop
                        buttonHard = newButton;
                        xboxItems[currentIndex] = newButton.ToString();
                    }
                    else if (currentIndex == 4)
                    { //flip left
                        buttonFlipLeft = newButton;
                        xboxItems[currentIndex] = newButton.ToString();
                    }
                    else if (currentIndex == 5)
                    { //flip right
                        buttonFlipRight = newButton;
                        xboxItems[currentIndex] = newButton.ToString();
                    }
                    changingKey = false;
                }
#else
                Keys[] newKeys = Keyboard.GetState().GetPressedKeys();
                if (currentIndex == 0)
                { //move left
                    keyLeft = newKeys[0];
                    controlItems[currentIndex] = keyLeft.ToString();
                }
                else if (currentIndex == 1)
                { //move right
                    keyRight = newKeys[0];
                    controlItems[currentIndex] = keyRight.ToString();
                }
                else if (currentIndex == 2)
                { //soft drop
                    keySoft = newKeys[0];
                    controlItems[currentIndex] = keySoft.ToString();
                }
                else if (currentIndex == 3)
                { //hard drop
                    keyHard = newKeys[0];
                    controlItems[currentIndex] = keyHard.ToString();
                }
                else if (currentIndex == 4)
                { //flip left
                    keyFlipLeft = newKeys[0];
                    controlItems[currentIndex] = keyFlipLeft.ToString();
                }
                else if (currentIndex == 5)
                { //flip right
                    keyFlipRight = newKeys[0];
                    controlItems[currentIndex] = keyFlipRight.ToString();
                }
                changingKey = false;

#endif

                keyPrevState = Keyboard.GetState();
                gpPrevState = GamePad.GetState(PlayerIndex.One);

                return Blokus.GameState.CONTROLS;
            }
            bool kUp = Keyboard.GetState().IsKeyDown(Keys.Up) && !keyPrevState.IsKeyDown(Keys.Up)
                || currGPState.IsButtonDown(Buttons.DPadUp) && !gpPrevState.IsButtonDown(Buttons.DPadUp);
            bool kDown = Keyboard.GetState().IsKeyDown(Keys.Down) && !keyPrevState.IsKeyDown(Keys.Down)
                || currGPState.IsButtonDown(Buttons.DPadDown) && !gpPrevState.IsButtonDown(Buttons.DPadDown);


            //check for menu movement

            if (kUp)
            {
                currentIndex--;
                if (currentIndex < 0)
                    currentIndex = 6;
            }
            if (kDown)
            {
                currentIndex++;
                if (currentIndex > 6)
                    currentIndex = 0;
            }
            bool kEnter = Keyboard.GetState().IsKeyDown(Keys.Enter) && !keyPrevState.IsKeyDown(Keys.Enter)
                || currGPState.IsButtonDown(Buttons.A) && !gpPrevState.IsButtonDown(Buttons.A);
            bool kEsc = Keyboard.GetState().IsKeyDown(Keys.Escape) && !keyPrevState.IsKeyDown(Keys.Escape)
                || currGPState.IsButtonDown(Buttons.B) && !gpPrevState.IsButtonDown(Buttons.B);

            if (kEnter)
            {
                if (currentIndex == 6)
                {
                    currentIndex = 0;
                    return Blokus.GameState.MENU;
                }
                else
                    changingKey = true;
            }
            if (kEsc)
            {
                currentIndex = 0;
                return Blokus.GameState.MENU;
            }

            //reset previous keyboard state
            keyPrevState = Keyboard.GetState();
            gpPrevState = GamePad.GetState(PlayerIndex.One);

            return Blokus.GameState.CONTROLS;
        }

        public override void Draw(GameTime gameTime)
        {
            //graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
            graphics.GraphicsDevice.SetRenderTarget(backgroundRenderTarget);

            spriteBatch.Begin();
            spriteBatch.Draw(texBG, new Rectangle(0, 0, 1280, 800), Color.White);
            //menu items
            for (int ct = 0; ct < menuItems.Count(); ct++)
            {
                Color clr = Color.White;
                Random random = new Random();
                if (currentIndex == ct)
                    clr = new Color((float)random.NextDouble(), (float)random.NextDouble(), (float)random.NextDouble());
                string menuStr = menuItems[ct];
                spriteBatch.DrawString(menuFont, menuStr, new Vector2(100, 200 + ct * 50), clr);
            }
            //control items
            for (int ct = 0; ct < controlItems.Count(); ct++)
            {
                Color clr = Color.White;
                Random random = new Random();
                if (currentIndex == ct && changingKey)
                {
#if XBOX360
                    clr = Color.White;
#else
                    clr = new Color((float)random.NextDouble(), (float)random.NextDouble(), (float)random.NextDouble());
#endif
                }
                string controlStr = controlItems[ct];
                spriteBatch.DrawString(menuFont, controlStr, new Vector2(400, 200 + ct * 50), clr);
            }
            //xbox items
            for (int ct = 0; ct < xboxItems.Count(); ct++)
            {
                Color clr = Color.White;
                Random random = new Random();
                if (currentIndex == ct && changingKey)
                {
#if XBOX360
                    clr = new Color((float)random.NextDouble(), (float)random.NextDouble(), (float)random.NextDouble());
#else
                    clr = Color.White;
#endif
                }
                string xboxStr = xboxItems[ct];
                spriteBatch.DrawString(menuFont, xboxStr, new Vector2(700, 200 + ct * 50), clr);
            }
            spriteBatch.End();

            //BackgroundEffects.drawBGEffects(gameTime, aspectRatio, backgroundRenderTarget);
        }
        public override void ResetPrevKeys()
        {
            keyPrevState = Keyboard.GetState();
            gpPrevState = GamePad.GetState(PlayerIndex.One);
        }

    }
}
